//
//  XCPictureView.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/12.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

//注册cell的表示
private let pictureCellId = "pictureCellId"

//间距
private let pictCellMagin : CGFloat = 5

class XCPictureView: UICollectionView {
    
    var imageURLs : [NSURL]?{
        
        didSet{
            
            let pictSize = calculateSize()
            
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pictSize)

            }
            
            //刷新数据
            reloadData()
            
        }
    }

    init(){
        
        //创建流水布局
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.minimumLineSpacing = 0
        
        super.init(frame: CGRectZero, collectionViewLayout: flowlayout)
        
        //注册cell
        registerClass(pictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        //设置代理
        self.dataSource = self
        
        calculateSize()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //定义一个方法  返回flowlayout的大小  需要根据图片的数量来确定
    private func calculateSize() -> CGSize{
        
        let imageCount = imageURLs?.count ?? 0
        
//        print("imageCount == \(imageCount)")
        
        //宽度
        let maxWidth = screenWith - 2 * statusMagin
        //cell的宽度
        let itemSize = (maxWidth - 2 * pictCellMagin) / 3
        
        //获取layout布局对象
        let layout = collectionViewLayout as? UICollectionViewFlowLayout
        
//        print(imageURLs?.count)
//        print(imageCount)
        
        layout?.itemSize = CGSizeMake(itemSize, itemSize)
        
        if imageCount == 0 {
        
            return CGSizeZero
        }
        
        if imageCount == 1 {
            
            let imageSize = CGSizeMake(150, 100)
            
            layout?.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            
            let w = itemSize * 2 + pictCellMagin
            
            return CGSizeMake(w, w)
        }
        
        
        //其他多张图片的时候
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let pictrueHeight = itemSize * row + (row - 1) * pictCellMagin
        
        return CGSizeMake(maxWidth, pictrueHeight)
        
    }
    
}


//数据源方法
extension XCPictureView : UICollectionViewDataSource{
    
    //数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! pictureCell
        
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
    
}

//定义collectionView 的cell
class pictureCell : UICollectionViewCell {
    
    //当给imageURL赋值时  为icon赋值
    var imageURL : NSURL?{
        
        didSet{
            
            iconView.sd_setImageWithURL(imageURL)
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        contentView.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    //懒加载视图
    lazy var iconView : UIImageView = {
        
        let icon = UIImageView()
        
        //设置视图的显示模式
        icon.contentMode = .ScaleAspectFill
        
        //手动设置剪裁
        icon.clipsToBounds = true
        
        return icon
    }()
}